package com.kaifamiao.io.d0922;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
import java.util.Set;

public class PropertiesDemo {

    public static void main(String[] args) {
        // Map  --> values()   keySet()  entrySet()
        Properties prop = new Properties();

//        prop.put("url", "jdbc:mysql://10.0.2.60:3306/kfm");
//        prop.put("driver", "com.mysql.cj.jdbc.Driver");
//        prop.put("user", "root");
//        prop.put("password", "");

//        Set<Object> keySet = prop.keySet();
//
//        for (Object key : keySet){
//            System.out.println(key + " : " + prop.get(key));
//        }

        InputStream in = PropertiesDemo.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            Class.forName(prop.getProperty("driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = prop.getProperty("url");
        String user = prop.getProperty("user");
        String password = prop.getProperty("password");

        try {
            Connection conn = DriverManager.getConnection(url, user, password);
            String sql = "SELECT * FROM emp";
            PreparedStatement statement = conn.prepareStatement(sql);
            // ename scott
            statement.execute();

            ResultSet rs = statement.getResultSet();
//            ResultSet rs = statement.executeQuery(sql);

            while(rs.next()){
                System.out.print(rs.getObject(1));
                System.out.print(rs.getObject(2));
                System.out.print(rs.getObject(3));
                System.out.print(rs.getObject(4));
                System.out.print(rs.getObject(5));
                System.out.print(rs.getObject(6));
                System.out.print(rs.getObject(7));
                System.out.print(rs.getObject(8));
                System.out.println();
            }

            rs.close();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
